<?
include_once("../../config.php");
?>




<?//正式开始操作
//

function curl_api($url,$method,$headers,$bodys){
// 创建连接
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); //表示不检查证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //信任任何证书
    curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //设置直接采用ipv4 防止走ipv6流程，会变慢
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);    // 强制使用 HTTP/1.0


// 发送请求
$response = curl_exec($curl);
curl_close($curl);

// 读取响应  result返回所有任务
$result = json_decode($response,true);

return $result;
}



function employee($userid)
{



//从api中获取的id
    $emp['532bf1e598af49033c16763c']="yuan";
    $emp['57fc5da9a8d89a1d2ea9de0e']="kate";
    $emp['58c8a56878bc02312fb6f6b0']="cara";
    $emp['5afe35de3dcfb100010515c9']="lin";
    $emp['55b599429d33933471ddd2eb']="fang";
    $emp['5466ac9aa79f0b7576c9b4d7']="ming";
    $emp['532a681483c4380f0baec3ff']="yang";
    $emp['6125edcc6259613aefbc04d7']="lala";
    $emp['541924d46be697041451c4ab']="ping";

    //下面是是通讯录拿到的id
    $emp['5c2f6791e944bc0001767cec']="yuan";
    $emp['5c31b16e7c08180001e8e4c5']="kate";
    $emp['5c31b16e7c08180001e8e4c7']="cara";
    $emp['5c31b16e7c08180001e8e4c8']="lin";
    $emp['5c31b16e7c08180001e8e4c6']="fang";
    $emp['5c31b16e7c08180001e8e4c3']="ming";
    $emp['5c31b16e7c08180001e8e4c2']="yang";
    $emp['62b9563db158fa296753a59b']="lala";
    $emp['5c31b16e7c08180001e8e4c4']="ping";

    return $emp[$userid];


}
?>













<?

//先select一个project

$_project=select_one_line_all_items($table_Prefix."teambition_project","name != '' and nid > 1 ");

print_r2($_project);




//==============================
//开始循环项目，获取他们的任务
//==============================

$url    = "https://open.teambition.com/api/task/tqlsearch";
$method = 'POST';

// 配置headers
$headers = array(
    'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfYXBwSWQiOiI2MDRjODMyN2E1NmUxNjc4Nzk2ZDVmODYiLCJpYXQiOiIxNjY4MjI2OTI3IiwiZXhwIjo0MDk4MTQwNTI3fQ.EUBJTgPGm1wbacNuiCU4rhs1Oz3jdu3QUE6SFF0dRAQ',
    'X-Tenant-Id:5c2f6791e944bc0001767ce8',
    'X-Tenant-Type:organization',
    'Content-Type:application/json'

);




    echo "开始查找项目".$_project['name']."<br/>";

    $bodys = [
        "pageSize" => 500,
        "tql" => "_projectId =".$_project['projectid']."",
        "orderBy" => "dueDate"
    ];


    $api_taks=curl_api($url,$method,$headers,$bodys);



    for($i=0;$i<=count($api_taks['result']);$i++)
    {
        if(
            ($api_taks['result'][$i]['isDone']==0) &&
            ($api_taks['result'][$i]['executorId']) &&
            ($api_taks['result'][$i]['dueDate'])
        )
        {
            $task[$i]['project']=$_project['name'];
            $task[$i]['name']=$api_taks['result'][$i]['content'];
            $task[$i]['executorId']=$api_taks['result'][$i]['executorId'];
            $task[$i]['dueDate']=substr($api_taks['result'][$i]['dueDate'],0,10);
            $task[$i]['executor']=employee($api_taks['result'][$i]['executorId']);

        }



    }








//开始筛选超时的任务
foreach ( $task as $k=>$v)
{


    if(date("Y-m-d")>$v['dueDate'])
    {
        $overtaks[]=$v;
        echo $k.":".$v['project']."=".$v['name'].">".date("Y-m-d")."-".$v['dueDate']."符合资格<br/>";


    }
    else
    {
        echo $k.":".$v['project']."=".$v['name'].">".date("Y-m-d")."-".$v['dueDate']."不计算在内<br/>";

    }

}




print_r2($overtaks);


//没有任务的project会导致出错
if(count($overtaks)){



$p['table']=$table_Prefix."teambition_task";
$p['items']=array("project","name","executorid","enddays","executor");
$p['values']=$overtaks;


echo $insert_sql=insert_datas($p);
$query = mysql_query($insert_sql) or die ("error:".mysql_error()); ;
mysql_close($con);
}


//最后把这个project删除掉，让下一轮执行的可以不会重复

echo  $del_str="delete from ".$table_Prefix."teambition_project where projectid = '".$_project['projectid']."'";
$query = mysql_query($del_str) or die ("error:".mysql_error()); ;


?>




